/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.javlov.vf;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import net.javlov.Option;
import net.javlov.State;


public class QMap<T> implements QFunction<T> {

	private static final long serialVersionUID = 2316814707778622989L;

	protected Map<State<? extends T>, TableOptionValues> table;
	
	protected QFunction<T> initializer;
	
	protected List<? extends Option<?>> optionPool;
	
	public QMap(List<? extends Option<?>> optionPool) {
		this(optionPool, 1000);
	}
	
	public QMap(List<? extends Option<?>> optionPool, int size) {
		table = new HashMap<State<? extends T>, TableOptionValues>(size);
		this.optionPool = optionPool;
	}
	
	public double getInitialValue(State<? extends T> s, Option<?> o) {
		if ( initializer == null )
			return 0;
		return initializer.getValue(s,o);
	}
	
	public void setInitializer(QFunction<T> qf) {
		initializer = qf;
	}
	
	public double getValue(State<? extends T> s, Option<?> o) {
		try {
			return getValues(s).get(o);
		} catch ( IneligibleOptionException e ) {
			throw new IneligibleOptionException(o + " not eligible in " + s);
		}
	}
	
	public void setValue(State<? extends T> s, Option<?> o, double val) {
		TableOptionValues vals = (TableOptionValues)getValues(s);
		vals.set(o, val);
	}

	public void setValues(State<? extends T> s, TableOptionValues vals) {
		table.put(s, vals);
	}
	
	@Override
	public void init() {
		table.clear();		
	}
	
	@Override
	public void reset() {
		//do nothing
	}

	@Override
	public Double queryValue(State<? extends T> s, Option<?> o) {
		OptionValues vals = table.get(s);
		if ( vals == null )
			return null;
		
		try {
			return vals.get(o);
		} catch ( IneligibleOptionException e ) {
			throw new IneligibleOptionException(o + " not eligible in " + s);
		}
	}

	@Override
	public void update(State<? extends T> s, Option<?> o, double delta) {
		OptionValues vals = table.get(s);
		if ( vals == null )
			throw new UnknownStateException(s.toString());
		vals.update(o, delta);
	}
	
	@Override
	public OptionValues getValues(State<? extends T> s) {
		TableOptionValues vals = table.get(s);
		if ( vals == null ) {
			vals = new TableOptionValues( s, optionPool, initializer );
			table.put(s, vals);
		}
		return vals;
	}
	
	public OptionValues queryValues(State<? extends T> s) {
		return table.get(s);
	}

	/**
	 * 
	 * @return number of states in the table
	 */
	public int size() {
		return table.size();
	}
	
	@Override
	public String toString() {
		String s = "";
		for ( Map.Entry<State<? extends T>, TableOptionValues> e : table.entrySet() )
			s += e.toString() + "\n";
		return s;
	}
	
	public Set<State<? extends T>> states() {
		return Collections.unmodifiableSet(table.keySet());
	}
	
	public Collection<TableOptionValues> values() {
		return Collections.unmodifiableCollection(table.values());
	}
	
	public Set<Entry<State<? extends T>, TableOptionValues>> entrySet() {
		return Collections.unmodifiableSet(table.entrySet());
	}
}
